package com.youlu.campus.service.english;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.english.ActivityGroup;
import com.youlu.campus.entity.english.ActivityQaFeeDetail;
import com.youlu.campus.entity.english.UserCompetitionFee;
import com.youlu.campus.entity.english.vo.request.ActivityFeeCreatedCommand;
import com.youlu.campus.entity.english.vo.response.ActivityQaFeeResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ActivityQaFeeService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Lazy
    @Autowired
    private UserCompetitionFeeService userCompetitionFeeService;


    public List<ActivityQaFeeResponse> findById(String userId, String activityGroupId) {
        ActivityGroup activityGroup = this.mongoTemplate.findById(activityGroupId, ActivityGroup.class);
        if (Objects.isNull(activityGroup)) {
            return null;
        }
        List<ActivityQaFeeDetail> activityFeeDetails = activityGroup.getActivityFeeDetails();
        if (CollectionUtils.isEmpty(activityFeeDetails)) {
            throw new BusinessException("该组未配置相关费用");
        }
        List<ActivityQaFeeResponse> allQaFeeList = activityFeeDetails.stream().map(activityQaFeeDetail -> {
            ActivityQaFeeResponse activityQaFeeResponse = new ActivityQaFeeResponse();
            BeanUtils.copyProperties(activityQaFeeDetail, activityQaFeeResponse);
            activityQaFeeResponse.setGroupName(activityGroup.getGroupName());
            activityQaFeeResponse.setActivityName(activityGroup.getActivityName());
            activityQaFeeResponse.setSort(activityQaFeeDetail.getSort());
            return activityQaFeeResponse;
        }).collect(Collectors.toList()).stream().sorted(Comparator.comparing(ActivityQaFeeResponse::getSort)).collect(Collectors.toList());
        //用户购买服务
        List<UserCompetitionFee> userList = userCompetitionFeeService.userBuyfindActivityGroupId(userId,
                activityGroupId);
        if (CollectionUtils.isNotEmpty(userList)) {
            log.info("【英语竞赛】-> {}", JSON.toJSONString(userList));
            //购买题库练习
            long count = userList.stream().filter(userCompetitionFee -> ActivityQaFeeDetail.QA_FEE.equals(userCompetitionFee.getFeeTypeCode())).count();
            if (count > 0) {
                return allQaFeeList.stream().filter(qaFee -> !ActivityQaFeeDetail.QA_FEE.equals(qaFee.getFeeTypeCode())).filter(qaFee -> !ActivityQaFeeDetail.BAO_MING_QA_FEE.equals(qaFee.getFeeTypeCode())).collect(Collectors.toList());
            }
            long baoMingCount = userList.stream().filter(userCompetitionFee -> ActivityQaFeeDetail.BAO_MING_FEE.equals(userCompetitionFee.getFeeTypeCode())).count();
            if (baoMingCount > 0) {
                return allQaFeeList.stream().filter(qaFee -> !ActivityQaFeeDetail.BAO_MING_FEE.equals(qaFee.getFeeTypeCode())).filter(qaFee -> !ActivityQaFeeDetail.BAO_MING_QA_FEE.equals(qaFee.getFeeTypeCode())).collect(Collectors.toList());
            }
        }
        return allQaFeeList;

    }


    public ActivityQaFeeResponse detail(String activityGroupId, String feeTypeCode) {
        ActivityGroup activityGroup = this.mongoTemplate.findById(activityGroupId, ActivityGroup.class);
        if (Objects.isNull(activityGroup)) {
            return null;
        }
        List<ActivityQaFeeDetail> activityFeeDetails = activityGroup.getActivityFeeDetails();
        if (CollectionUtils.isEmpty(activityFeeDetails)) {
            return null;
        }
        activityFeeDetails.stream().sorted(Comparator.comparing(ActivityQaFeeDetail::getSort)).collect(Collectors.toList());
        Optional<ActivityQaFeeDetail> feeDetailOptional =
                activityFeeDetails.stream().filter(qaFee -> qaFee.getFeeTypeCode().equals(feeTypeCode)).findFirst();
        if (feeDetailOptional.isPresent()) {
            ActivityQaFeeResponse activityQaFeeResponse = new ActivityQaFeeResponse();
            BeanUtils.copyProperties(feeDetailOptional.get(), activityQaFeeResponse);
            activityQaFeeResponse.setGroupName(activityGroup.getGroupName());
            activityQaFeeResponse.setActivityName(activityGroup.getActivityName());
            activityQaFeeResponse.setActivityGroupId(activityGroup.getId());
            activityQaFeeResponse.setGroupName(activityGroup.getGroupName());
            activityQaFeeResponse.setActivityName(activityGroup.getActivityName());
            return activityQaFeeResponse;
        }
        return null;

    }

    public Page<ActivityQaFeeResponse> list() {
        Query query = new Query();
        List<ActivityGroup> list = this.mongoTemplate.find(query, ActivityGroup.class);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<ActivityQaFeeResponse> result = new ArrayList<>();
        list.stream().forEach(activityGroup -> {
            List<ActivityQaFeeDetail> activityFeeDetails = activityGroup.getActivityFeeDetails();
            if (CollectionUtils.isNotEmpty(activityFeeDetails)) {
                List<ActivityQaFeeResponse> qaFeeList = activityFeeDetails.stream().map(activityQaFeeDetail -> {
                    ActivityQaFeeResponse activityQaFeeResponse = new ActivityQaFeeResponse();
                    activityQaFeeResponse.setActivityId(activityGroup.getActivityId());
                    activityQaFeeResponse.setActivityName(activityGroup.getActivityName());
                    activityQaFeeResponse.setActivityGroupId(activityGroup.getId());
                    activityQaFeeResponse.setGroupName(activityGroup.getGroupName());
                    activityQaFeeResponse.setDescription(activityQaFeeDetail.getDescription());
                    activityQaFeeResponse.setFeeName(activityQaFeeDetail.getFeeName());
                    activityQaFeeResponse.setFeeTypeCode(activityQaFeeDetail.getFeeTypeCode());
                    activityQaFeeResponse.setPrice(activityQaFeeDetail.getPrice());
                    activityQaFeeResponse.setSort(activityQaFeeDetail.getSort());
                    return activityQaFeeResponse;
                }).collect(Collectors.toList());
                result.addAll(qaFeeList);
            }
        });
        return new PageImpl<>(result);
    }

    public void save(ActivityFeeCreatedCommand createdCommand) {
        String activityGroupId = createdCommand.getActivityGroupId();
        ActivityGroup activityGroup = this.mongoTemplate.findById(activityGroupId, ActivityGroup.class);
        if (Objects.isNull(activityGroup)) {
            return;
        }
        List<ActivityQaFeeDetail> activityFeeDetails = activityGroup.getActivityFeeDetails();
        if (CollectionUtils.isEmpty(activityFeeDetails)) {
            activityFeeDetails = new ArrayList<>();
        }
        buildQaFee(activityFeeDetails, createdCommand);
        activityGroup.setActivityFeeDetails(activityFeeDetails);
        log.info("activityFeeDetails - >{}", activityFeeDetails.size());
        this.mongoTemplate.save(activityGroup);
    }

    private void buildQaFee(List<ActivityQaFeeDetail> activityFeeDetails,
                            ActivityFeeCreatedCommand createdCommand) {
        String feeTypeCode = createdCommand.getFeeTypeCode();
        ActivityQaFeeDetail activityQaFeeDetail = null;
        switch (feeTypeCode) {
            case ActivityQaFeeDetail.BAO_MING_FEE:
                activityQaFeeDetail = new ActivityQaFeeDetail();
                activityQaFeeDetail.setFeeTypeCode(ActivityQaFeeDetail.BAO_MING_FEE);
                break;
            case ActivityQaFeeDetail.BAO_MING_QA_FEE:
                activityQaFeeDetail = new ActivityQaFeeDetail();
                activityQaFeeDetail.setFeeTypeCode(ActivityQaFeeDetail.BAO_MING_QA_FEE);
                break;
            case ActivityQaFeeDetail.QA_FEE:
                activityQaFeeDetail = new ActivityQaFeeDetail();
                activityQaFeeDetail.setFeeTypeCode(ActivityQaFeeDetail.QA_FEE);
                break;
            default:
        }
        activityQaFeeDetail.setFeeName(createdCommand.getFeeName());
        activityQaFeeDetail.setDescription(createdCommand.getDescription());
        activityQaFeeDetail.setPrice(createdCommand.getPrice());
        if (CollectionUtils.isEmpty(activityFeeDetails)) {
            activityFeeDetails.add(activityQaFeeDetail);
            return;
        }
        ActivityQaFeeDetail finalActivityQaFeeDetail = activityQaFeeDetail;
        activityFeeDetails.stream().map(activityQaFee -> {
            if (activityQaFee.getFeeTypeCode().equals(feeTypeCode)) {
                activityQaFee.setPrice(finalActivityQaFeeDetail.getPrice());
                activityQaFee.setDescription(finalActivityQaFeeDetail.getDescription());
                return activityQaFee;
            }
            return activityQaFee;
        }).collect(Collectors.toList());
        long count =
                activityFeeDetails.stream().filter(activityQaFee -> activityQaFee.getFeeTypeCode().equals(feeTypeCode)).count();
        if (count == 0) {
            activityFeeDetails.add(activityQaFeeDetail);
        }

    }


}
